package com.jrx.anytxn.accounting;

import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;

/**
 * @program: anytxn-cf-transaction
 * @description:
 * @author: xiaoyu.jing
 * @date: 2020-03-27 14:14
 **/
public class CreateBatchXml {

    public static void main(String args[]) throws Exception {

        transferInsert("com.jrx.anytxn.accounting.entity.GaVoucherExcpReport");

    }

    public static void transferInsert(String className) throws ClassNotFoundException {
        Class a = Class.forName(className);
        StringBuilder sb = new StringBuilder();
        sb.append("<insert id=\"batchInsert\" parameterType=\"ArrayList\">\n");
        sb.append("insert into ").append(ModelToTable(a.getSimpleName())).append("(\n");
        Field[] fields = a.getDeclaredFields();
        int i = 0;
        for(Field field : fields){
            if(i % 5 == 0 && i != 0){
                sb.append("\n");
            }
            if("create_time".equals(propertyToField(field.getName()))){
                continue;
            }
            if("update_time".equals(propertyToField(field.getName()))){
                continue;
            }
            sb.append(propertyToField(field.getName())).append(",");
            i++;
        }
        sb = new StringBuilder(sb.substring(0,sb.length()-1));
        sb.append(")\n");
        sb.append("values\n");
        sb.append("<foreach collection=\"list\" item=\"item\" separator=\",\">\n");
        sb.append("(");
        int j = 0;
        for(Field field : fields){
            if(j % 5 == 0 && j != 0){
                sb.append("\n");
            }
            if("create_time".equals(propertyToField(field.getName()))){
                continue;
            }
            if("update_time".equals(propertyToField(field.getName()))){
                continue;
            }
            sb.append("#{item.").append(getFiledName(field.getName())).append(",").append("jdbcType=");
            if(field.getType().getName().equals("java.lang.Long")){
                sb.append("BIGINT");
            }
            if(field.getType().getName().equals("java.lang.Integer")){
                sb.append("INTEGER");
            }
            if(field.getType().getName().equals("java.lang.String")){
                sb.append("VARCHAR");
            }
            if(field.getType().getName().equals("java.math.BigDecimal")){
                sb.append("DECIMAL");
            }
            if(field.getType().getName().equals("java.util.Date")){
                sb.append("DATE");
            }
            if(field.getType().getName().equals("java.sql.Timestamp")){
                sb.append("TIMESTAMP");
            }
            sb.append("},");
            j++;
        }
        sb = new StringBuilder(sb.substring(0,sb.length()-1));
        sb.append(")\n");
        sb.append(" </foreach>\n");
        sb.append(" </insert>\n");
        System.out.println(sb);
    }

    public static String ModelToTable(String property) {
        if (null == property) {
            return "";
        }
        char[] chars = property.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (char c : chars) {
            if (CharUtils.isAsciiAlphaUpper(c)) {
                sb.append("_" + StringUtils.lowerCase(CharUtils.toString(c)));
            } else {
                sb.append(c);
            }
        }
        return sb.toString().substring(1,sb.length());
    }

    public static String propertyToField(String propertys) {
        String[] propertyArray = propertys.split(".");
        String property = propertys;
        if(propertyArray.length>0){
            property = propertyArray[propertyArray.length-1];
        }

        if (null == property) {
            return "";
        }
        char[] chars = property.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (char c : chars) {
            if (CharUtils.isAsciiAlphaUpper(c)) {
                sb.append("_" + StringUtils.lowerCase(CharUtils.toString(c)));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String getFiledName(String propertys) {
        String[] propertyArray = propertys.split(".");
        String property = propertys;
        if(propertyArray.length>0){
            property = propertyArray[propertyArray.length-1];
        }
        return property;
    }
}
